{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3/10 3/10]\n",
      " [3/10 1/10]]\n",
      "Px:[[3/5]\n",
      " [2/5]]\n",
      "Py:[[3/5 2/5]]\n"
     ]
    }
   ],
   "source": [
    "#例3-6，3-7\n",
    "import numpy as np\n",
    "from utility import margDist\n",
    "from sympy import Rational as R\n",
    "Pxy=np.array([[R(3,10), R(3,10)],\n",
    "             [R(3,10), R(1,10)]])\n",
    "print(Pxy)\n",
    "Px, Py=margDist(Pxy)\n",
    "print('Px:%s'%Px)\n",
    "print('Py:%s'%Py)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1/4]\n",
      " [1/4]\n",
      " [1/4]\n",
      " [1/4]] [[1/2 1/3 1/6]]\n",
      "P(Y|X=4):\n",
      "[1/3 1/3 1/3]\n",
      "P(X|Y=3):\n",
      "[1/2 0 0 1/2]\n",
      "X与Y相互独立是False\n"
     ]
    }
   ],
   "source": [
    "#练习3-6，3-7，3-8，3-9\n",
    "import numpy as np\n",
    "from sympy import Rational as R\n",
    "from utility import margDist, condDist, independent\n",
    "Pxy=np.array([[R(1,12), R(1,12), R(1,12)],\n",
    "             [R(1,6), R(1,12), R(0,1)],\n",
    "             [R(1,6), R(1,12), R(0,1)],\n",
    "             [R(1,12), R(1,12), R(1,12)]])\n",
    "Px, Py=margDist(Pxy)\n",
    "print(Px, Py)\n",
    "X_Y, Y_X=condDist(Pxy)\n",
    "print('P(Y|X=4):\\n%s'%Y_X[3])\n",
    "print('P(X|Y=3):\\n%s'%X_Y[:,2])\n",
    "print('X与Y相互独立是%s'%independent(Pxy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(X|Y=0):[1/2 1/2]\n",
      "P(Y|X=1):[3/4 1/4]\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#例3-8\n",
    "import numpy as np\n",
    "from utility import condDist, independent\n",
    "from sympy import Rational as R\n",
    "Pxy=np.array([[R(3,10), R(3,10)],\n",
    "             [R(3,10), R(1,10)]])\n",
    "Px_y, Py_x=condDist(Pxy)\n",
    "print('P(X|Y=0):%s'%Px_y[:,0])\n",
    "print('P(Y|X=1):%s'%Py_x[1])\n",
    "print(independent(Pxy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "无放回抽样，X与Y相互独立是False\n",
      "有放回抽样，X与Y相互独立是True\n"
     ]
    }
   ],
   "source": [
    "#例3-9\n",
    "import numpy as np\n",
    "from utility import independent\n",
    "from sympy import Rational as R\n",
    "Pxy=np.array([[R(3,10), R(3,10)],\n",
    "              [R(3,10), R(1,10)]])\n",
    "print('无放回抽样，X与Y相互独立是%s'%independent(Pxy))\n",
    "Pxy=np.array([[9/25, 6/25],\n",
    "              [6/25, 4/25]])\n",
    "print('有放回抽样，X与Y相互独立是%s'%independent(Pxy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(X>=3/4)=0.5781\n"
     ]
    }
   ],
   "source": [
    "#例3-17\n",
    "from scipy.integrate import dblquad\n",
    "f=lambda y, x: 3*x\n",
    "g1=lambda x: 0\n",
    "g2=lambda x: x\n",
    "p, _=dblquad(f, 3/4, 1, g1, g2)\n",
    "print('P(X>=3/4)=%.4f'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(X>=3/4)=0.015625\n"
     ]
    }
   ],
   "source": [
    "#练习3-10\n",
    "from scipy.integrate import dblquad\n",
    "f=lambda y, x: 3*x\n",
    "g1=lambda x: 0\n",
    "g2=lambda x: x\n",
    "p, _=dblquad(f, 0, 1/4, g1, g2)\n",
    "print('P(X>=3/4)=%f'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(0.5<X<1.5)=0.75\n",
      "P(0.1<Y≤0.4|x=1.5)=0.60\n"
     ]
    }
   ],
   "source": [
    "#例3-18\n",
    "from scipy.integrate import quad\n",
    "fx=lambda x: x if x<=1 else 2-x\n",
    "p, _=quad(fx, 0.5, 1.5)\n",
    "print('P(0.5<X<1.5)=%.2f'%p)\n",
    "f=lambda x: 2\n",
    "p, _=quad(f, 0.1, 0.4)\n",
    "print('P(0.1<Y≤0.4|x=1.5)=%.2f'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(1.1<X<1.5)=0.4000\n"
     ]
    }
   ],
   "source": [
    "#练习3-16\n",
    "from scipy.integrate import quad\n",
    "f=lambda x: 1\n",
    "p,_=quad(f,1.1,1.5)\n",
    "print('P(1.1<X<1.5)=%.4f'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(|X-Y|<5)=0.1667\n"
     ]
    }
   ],
   "source": [
    "#例3-19\n",
    "from utility import appointment\n",
    "p=appointment(15, 45, 0, 60, 5)\n",
    "print('P(|X-Y|<5)=%.4f'%p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P(|X-Y|<0.0833)=0.0208\n"
     ]
    }
   ],
   "source": [
    "#练习3-17\n",
    "from utility import appointment\n",
    "p=appointment(7, 9,  8, 12, 1/12)\n",
    "print('P(|X-Y|<%.4f)=%.4f'%(1/12, p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  0  1  3  5] [0.1 0.2 0.3 0.1 0.3]\n",
      "[-1  0  1  2] [0.4 0.2 0.1 0.3]\n"
     ]
    }
   ],
   "source": [
    "#练习3-18\n",
    "import numpy as np\n",
    "from utility import distCalcu\n",
    "X=(np.array([-1, 2])).reshape(2,1)\n",
    "Y=(np.array([0, 1, 2])).reshape(1, 3)\n",
    "Pxy=np.array([[0.1,0.2,0.1],[0.2,0.1,0.3]])\n",
    "P1=Pxy.reshape(6,)\n",
    "Z=(X*Y+1).reshape(6,)\n",
    "Z,P=distCalcu(Z, P1)\n",
    "print(Z,P)\n",
    "W=(np.minimum(X,Y)).reshape(6,)\n",
    "W,P=distCalcu(W, P1)\n",
    "print(W, P)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "单侧左、右分位点：a=-1.6449, b=1.6449\n",
      "双侧左、右分位点：a=-1.9600, b=1.9600\n"
     ]
    }
   ],
   "source": [
    "#例3-31\n",
    "from scipy.stats import norm\n",
    "alpha=0.05\n",
    "b=norm.isf(q=alpha)\n",
    "a=-b\n",
    "print('单侧左、右分位点：a=%.4f, b=%.4f'%(a, b))\n",
    "a, b=norm.interval(1-alpha)\n",
    "print('双侧左、右分位点：a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "单侧左、右分位点：a=-1.2816, b=1.2816\n",
      "双侧左、右分位点：a=-1.6449, b=1.6449\n"
     ]
    }
   ],
   "source": [
    "#练习3-27\n",
    "from scipy.stats import norm\n",
    "alpha=0.1\n",
    "b=norm.isf(q=alpha)\n",
    "a=-b\n",
    "print('单侧左、右分位点：a=%.4f, b=%.4f'%(a, b))\n",
    "a, b=norm.interval(1-alpha)\n",
    "print('双侧左、右分位点：a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "单侧左、右分位点：a=13.8484, b=36.4150\n",
      "双侧左、右分位点：a=12.4012, b=39.3641\n"
     ]
    }
   ],
   "source": [
    "#例3-32\n",
    "from scipy.stats import chi2\n",
    "n=24\n",
    "alpha=0.05\n",
    "a=chi2.ppf(q=alpha, df=n)\n",
    "b=chi2.isf(q=alpha, df=n)\n",
    "print('单侧左、右分位点：a=%.4f, b=%.4f'%(a, b))\n",
    "a, b=chi2.interval(1-alpha, df=n)\n",
    "print('双侧左、右分位点：a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "单侧左、右分位点：a=22.2706, b=42.5847\n",
      "双侧左、右分位点：a=20.0719, b=46.1943\n"
     ]
    }
   ],
   "source": [
    "#练习3-28\n",
    "from scipy.stats import chi2\n",
    "n=32\n",
    "alpha=0.1\n",
    "a=chi2.ppf(q=alpha, df=n)\n",
    "b=chi2.isf(q=alpha, df=n)\n",
    "print('单侧左、右分位点：a=%.4f, b=%.4f'%(a, b))\n",
    "a, b=chi2.interval(1-alpha, df=n)\n",
    "print('双侧左、右分位点：a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "单侧左、右分位点：a=-1.3086, b=1.3086\n",
      "双侧左、右分位点：a=-1.6939, b=1.6939\n"
     ]
    }
   ],
   "source": [
    "#例3-33\n",
    "from scipy.stats import t\n",
    "n=32\n",
    "alpha=0.1\n",
    "b=t.isf(q=alpha, df=n)\n",
    "a=-b\n",
    "print('单侧左、右分位点：a=%.4f, b=%.4f'%(a, b))\n",
    "a, b=t.interval(1-alpha, df=n)\n",
    "print('双侧左、右分位点：a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "单侧左、右分位点：a=-1.7109, b=1.7109\n",
      "双侧左、右分位点：a=-2.0639, b=2.0639\n"
     ]
    }
   ],
   "source": [
    "#练习3-29\n",
    "from scipy.stats import t\n",
    "n=24\n",
    "alpha=0.05\n",
    "b=t.isf(q=alpha, df=n)\n",
    "a=-b\n",
    "print('单侧左、右分位点：a=%.4f, b=%.4f'%(a, b))\n",
    "a, b=t.interval(1-alpha, df=n)\n",
    "print('双侧左、右分位点：a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "单侧左、右分位点：a=0.3576, b=3.0729\n",
      "双侧左、右分位点：a=0.2910, b=3.8682\n"
     ]
    }
   ],
   "source": [
    "#例3-34\n",
    "from scipy.stats import f\n",
    "m=12\n",
    "n=9\n",
    "alpha=0.05\n",
    "a=f.ppf(q=alpha, dfn=m, dfd=n)\n",
    "b=f.isf(q=alpha, dfn=m, dfd=n)\n",
    "print('单侧左、右分位点：a=%.4f, b=%.4f'%(a, b))\n",
    "a, b=f.interval(1-alpha, dfn=m, dfd=n)\n",
    "print('双侧左、右分位点：a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "单侧左、右分位点：a=0.6106, b=1.6721\n",
      "双侧左、右分位点：a=0.5298, b=1.9390\n"
     ]
    }
   ],
   "source": [
    "#练习3-30\n",
    "from scipy.stats import f\n",
    "m=30\n",
    "n=24\n",
    "alpha=0.1\n",
    "a=f.ppf(q=alpha, dfn=m, dfd=n)\n",
    "b=f.isf(q=alpha, dfn=m, dfd=n)\n",
    "print('单侧左、右分位点：a=%.4f, b=%.4f'%(a, b))\n",
    "a, b=f.interval(1-alpha, dfn=m, dfd=n)\n",
    "print('双侧左、右分位点：a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
